<?php 
/**
 * @author Eugenio Fage
 */
App::import('component','Ban');
App::import('model','Ban');

class FakeController {}

class BanTestCase extends CakeTestCase {
    var $fixtures = array('ban');  

    
    
    function getBanComponent(){
    	$ban= new BanComponent(); 
		$ban->initialize(new FakeController());
             
        $ban->user_ip="100.0.0.2";
        $ban->user_proxy=null;
        return $ban;
    }
    
    function testGet(){
    	$ban=$this->getBanComponent();
    	
    	$this->assertTrue('100.0.0.2'==$ban->getUserIp());
    	$this->assertTrue(null==$ban->getUserProxy());
    }
    
    function testMailBan() { 
          $ban=$this->getBanComponent();  
    	
          $this->assertTrue($ban->notBannedEmail('papa@gmail.com','domain'));
          $this->assertTrue($ban->notBannedEmail('papa@gmail.com','mailbox'));
          $this->assertTrue($ban->notBannedEmail('papa@gmail.com'));
                    
          $this->assertTrue($ban->notBannedEmail('spammer@newdomain.com'));
          $this->assertTrue($ban->notBannedEmail('spammer@newdomain.com','domain'));
          $this->assertFalse($ban->notBannedEmail('spammer@newdomain.com','mailbox'));
          
          $this->assertTrue($ban->notBannedEmail('clon@spammer.com'));
          $this->assertTrue($ban->notBannedEmail('clon@spammer.com','mailbox'));
          $this->assertFalse($ban->notBannedEmail('papa@spammer.com','domain'));
          $this->assertTrue($ban->notBannedEmail('papa@gmail.com'));
	}
	
	function testProxyBan(){
		$ban=$this->getBanComponent();
		
		$this->assertFalse($ban->notBannedProxy("127.0.0.1"));
		
		// check A.B.C
		$this->assertFalse($ban->notBannedProxy("127.0.22.1"));
		$this->assertTrue($ban->notBannedProxy("127.0.11.33"));
		
		//check A.B
		$this->assertTrue($ban->notBannedProxy("200.11.33.2"));
		$this->assertFalse($ban->notBannedProxy("127.11.33.6"));
	}
	
	function testIpBan(){
		$ban=$this->getBanComponent();
		
		$this->assertTrue($ban->notBannedIp("127.0.0.1"));
		$this->assertTrue($ban->notBannedIp("127.0.0.1",true));
		
		$this->assertFalse($ban->notBannedIp("192.0.0.1"));
	}
	
	function testHostnameBanned(){
		$ban=$this->getBanComponent();
		
		$this->assertTrue($ban->notBannedHostname('google.com'));
		$this->assertTrue($ban->notBannedHostname('wordpress.com'));
		
		$this->assertFalse($ban->notBannedHostname('spam.wordpress.com',false));
		$this->assertFalse($ban->notBannedHostname('spam.com',false));
		$this->assertFalse($ban->notBannedHostname('blog.spam.com'));
		$this->assertFalse($ban->notBannedHostname('splog.spam.com'));
		
		$this->assertTrue($ban->notBannedHostname('splog.spam.com',false));
		
		$this->assertTrue($ban->notBannedHostname('splog.spam.com/blog',false));
		$this->assertFalse($ban->notBannedHostname('splog.spam.com/blog'));
		
		$comment="hey this code is fucking shit";
		$this->assertFalse($ban->notBannedWord($comment));
		$comment="hey this code is good";
		$this->assertTrue($ban->notBannedWord($comment));
			
	}
}
